package com.wuzhipeng.heart.user.xxl_job;

import com.wuzhipeng.heart.user.convention.exception.ClientException;
import com.wuzhipeng.heart.user.dao.entity.Appointments;
import com.wuzhipeng.heart.user.service.IAppointmentsService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequiredArgsConstructor
@Component
public class xxl_jobHandler {
    private static final Logger log = LoggerFactory.getLogger(xxl_jobHandler.class);
    private final IAppointmentsService appointmentsService;

    /**
     * 定时任务，每天每隔一小时执行一次，检查过期预约，并删除
     */
    @XxlJob(value = "testHandler")
    public void testHandler() {
        log.info("开始执行定时任务");
        List<Appointments> allAppointments = appointmentsService.getAllAppointmentsByDate();
        List<Appointments> list = allAppointments.stream().filter(appointment -> appointment.getConsultationStatus() == 0).toList();
        List<Integer> ids = new ArrayList<>();
        for (Appointments appointment : allAppointments) {
            SimpleDateFormat df = new SimpleDateFormat("HH:mm");
            try {
                Date nowTime = df.parse(df.format(new Date()));
                String[] timeStr = appointment.getConsultationTime().split("-");
                String endTime = timeStr[1];
                Date endTimeDate = df.parse(endTime);
                if (nowTime.after(endTimeDate)) {
                    ids.add(appointment.getAppointmentID());
                }
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            appointmentsService.deleteExpiredAppointments(ids);
        }
    }

    /**
     * 定时任务，每天每隔一小时执行一次，检查过期预约，并删除
     */
    @XxlJob(value = "testHandler1")
    public void testHandler1() {
        log.info("开始执行定时任务");
        List<Appointments> allAppointments = appointmentsService.getAllAppointmentsByDate();
        List<Appointments> list = allAppointments.stream().filter(appointment -> appointment.getConsultationStatus() == 1).toList();
        List<Appointments> listAppointments = new ArrayList<>();
        for (Appointments appointment : allAppointments) {
            SimpleDateFormat df = new SimpleDateFormat("HH:mm");
            try {
                Date nowTime = df.parse(df.format(new Date()));
                String[] timeStr = appointment.getConsultationTime().split("-");
                String endTime = timeStr[1];
                Date endTimeDate = df.parse(endTime);
                if (nowTime.after(endTimeDate)) {
                    appointment.setConsultationStatus(3);
                    listAppointments.add(appointment);
                }
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            appointmentsService.updateBatchById(listAppointments);
        }
    }

}
